package com.hnkywl.sts.action.cwgl.yhsxdr;

import com.google.gson.Gson;
import com.hnkywl.sts.dao.cwgl.VoucherType;
import com.hnkywl.sts.dao.zjgl.YhsxmxDao;
import com.hnkywl.sts.entity.cwgl.VoucherItemData;
import com.hnkywl.sts.entity.sys.Yg;
import com.hnkywl.sts.service.cwgl.VoucherService;
import com.hnkywl.sts.service.cwgl.kmys.YhsxdrService;
import com.hnkywl.sts.service.sys.YgService;
import com.hnkywl.sts.util.UUIDGenerator;
import com.hnkywl.sts.util.XmlParse;
import net.ruixin.web.struts2.SimpleActionSupport;
import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.springframework.beans.factory.annotation.Autowired;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created with IntelliJ IDEA.
 * User: lenovo
 * Date: 13-6-3
 * Time: 下午3:14
 * To change this template use File | Settings | File Templates.
 */
@Namespace("/cwgl/yhsxdr")
public class YhsxdrAction extends SimpleActionSupport {
    private YhsxdrService yhsxdrService;

    //分页组件 起始位置
    private int page;
    //分页组件 页面大小
    private int rows;
    //行索引
    private int index;
    // 凭证号
    private String voucherNum;
    //操作人
    private String czr;
    //银行授信导入Id
    private Long yhsxdrId;
    //状态
    private Integer yhsxdrZt;

    private YgService ygService;
    //员工
    private Yg yg;

    //公司机构名称
    private String gsMc;
    //公司机构Id
    private Long gsId;
    private Date jkrqStart;
    private Date jkrqEnd;

    private Map<String, Object> voucherMaster = new HashMap<String, Object>();

    private Integer attachment;

    private VoucherService voucherService;

    private XmlParse xmlParse;

    private Map<String, String> result = new HashMap<String, String>();

    private Gson gson = new Gson();

    private Date yhsxdrSystemDate;

    private Map<String, Object> voucher;

    private List<VoucherItemData> voucherItems;

    private String bz;

    private YhsxmxDao yhsxmxDao;

    private String systemDate;


    //贴现承兑查询分页显示
    @Action("yhsxdrList")
    public void yhsxdrList() {
        this.renderJson(this.yhsxdrService.findAllYhsxdr(gsId, gsMc, jkrqStart, jkrqEnd,yhsxdrZt, page, rows));
    }
    //银行授信进入票据页面
    @Action("yhsxdr_insert")
    public String yhsxdrInsert(){
        yg = ygService.findYgDetail(YgService.getCurrentYg().getId());
        gsMc = this.yhsxmxDao.get(yhsxdrId).getYhsx().getNbgs().getJc();
        DateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
        systemDate =fmt.format(new Date()) ;
        voucherMaster = this.yhsxdrService.getVoucherMaster(yhsxdrId, attachment, null, null, null, yg);
        return SUCCESS;
    }

    @Action("findyhsxdrfl")
    public void findyhsxdrFl(){
        renderText(yhsxdrService.findyhsxdrFl(yhsxdrId));
    }
    //银行导入
    @Action("generate_voucher")
    public void generateVoucher() {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat simpleDateFormat2 = new SimpleDateFormat("yyyy");
        SimpleDateFormat simpleDateFormat3 = new SimpleDateFormat("MM");
        try {
            if (StringUtils.isEmpty(YgService.getCurrentYgJiuqiUsername())) {
                throw new RuntimeException("您没有财务系统用户名，不具有导入权限");
            }
            if (StringUtils.isEmpty(YgService.getCurrentYgJiuqiPassword())) {
                throw new RuntimeException("您没有财务系统密码，不具有导入权限");
            }
            String uuid = UUIDGenerator.getUUID();
            Integer year = GregorianCalendar.getInstance().get(Calendar.YEAR);
            Integer mouth = GregorianCalendar.getInstance().get(Calendar.MONTH);
            yg = ygService.findYgDetail(YgService.getCurrentYg().getId());
            Boolean begin = voucherService.beginGenerateVoucher(yhsxdrId, VoucherType.YHSXMX);
            if (!begin)
                throw new RuntimeException("其他用户已经开始导入该凭证，请选择其他凭证进行导入!");
            //凭证分录
            voucher = this.yhsxdrService.getU8VoucherItems(yhsxdrId, uuid, voucherItems);
            //凭证头
            Map<String, Object> msgheader = voucherService.getMsgHeader();
            voucher.put("msgheader", msgheader);
            //凭证主表信息
            Map<String, Object> voucherMaster =
                    this.yhsxdrService.getVoucherMaster(yhsxdrId, attachment, uuid, year.toString(), mouth.toString(), yg);
            //将头部可以修改的部分的修改值重新赋值
            voucherMaster.put("createdate",simpleDateFormat.format(yhsxdrSystemDate));
            voucherMaster.put("acctyear",simpleDateFormat2.format(yhsxdrSystemDate));
            voucherMaster.put("acctperiod",simpleDateFormat3.format(yhsxdrSystemDate));
            voucherMaster.put("attachment",attachment);
            voucherMaster.put("memo",bz);
            voucher.put("master", voucherMaster);
            String r = voucherService.generateVoucher(voucher);
            result = xmlParse.getContent(r);
            if ("1".equals(result.get(XmlParse.RESPONSEFLAG))) {
                String vchrid = result.get(XmlParse.VCHRID);
                String vchrnum = result.get(XmlParse.VCHRNUM);
                Boolean success = voucherService.generateVoucherSuccess(uuid, vchrid, vchrnum, year, yhsxdrId, VoucherType.YHSXMX);
                if (!success)
                    throw new RuntimeException("凭证导入成功，但凭证信息回写失败！");
            }
        } catch (Exception e) {
            e.printStackTrace();
            result.put(XmlParse.RESPONSEFLAG, "0");
            result.put(XmlParse.RESPONSEMESSAGE, e.getMessage());
        }
        renderJson(gson.toJson(result));
    }


    /**
     * 取消
     */
    @Action("revoke_note")
    public void revokeNote() {
        String error = this.yhsxdrService.revokeNote(yhsxdrId);
        if (error != null) {
            result.put(XmlParse.RESPONSEFLAG, "0");
            result.put(XmlParse.RESPONSEMESSAGE, error);
        } else {
            result.put(XmlParse.RESPONSEFLAG, "1");
            result.put(XmlParse.RESPONSEMESSAGE, "取消成功");
        }
        renderJson(gson.toJson(result));
    }

    /**
     * 撤销凭证
     */
    @Action("revoke_voucher")
    public void revokeVoucher() {
        try {
            Boolean begin = voucherService.beginRevokeVoucher(yhsxdrId, VoucherType.YHSXMX);
            if (!begin)
                throw new RuntimeException("其他用户已经开始撤销该凭证，请选择其他凭证进行撤销!");
            Map<String, Object> msgheader = voucherService.getMsgHeader();
            Map<String, Object> revokeInfo = yhsxdrService.getRevokeInfo(yhsxdrId);
            Map<String, Object> info = new HashMap<String, Object>();
            info.put("msgheader", msgheader);
            info.put("revokeinfo", revokeInfo);
            String r = voucherService.revokeVoucher(info);
            result = xmlParse.getContent(r);
            if ("1".equals(result.get(XmlParse.RESPONSEFLAG))) {
                Boolean success = voucherService.revokeVoucherSuccess(yhsxdrId, VoucherType.YHSXMX);
                if (!success)
                    throw new RuntimeException("凭证撤销成功，但凭证状态回写失败！");
            }
        } catch (Exception e) {
            e.printStackTrace();
            result.put(XmlParse.RESPONSEFLAG, "0");
            result.put(XmlParse.RESPONSEMESSAGE, e.getMessage());
        }
        renderJson(gson.toJson(result));
    }

    /**
     * 重置凭证
     */
    @Action("rest_voucher")
    public void restVoucher() {
        renderJson(voucherService.restVoucher(yhsxdrId, VoucherType.YHSXMX));
    }

    //==================get and set ================================


    public int getPage() {
        return page;
    }

    public void setPage(int page) {
        this.page = page;
    }

    public int getRows() {
        return rows;
    }

    public void setRows(int rows) {
        this.rows = rows;
    }

    public int getIndex() {
        return index;
    }

    public void setIndex(int index) {
        this.index = index;
    }

    public Long getGsId() {
        return gsId;
    }

    public void setGsId(Long gsId) {
        this.gsId = gsId;
    }

    public Integer getYhsxdrZt() {
        return yhsxdrZt;
    }

    public void setYhsxdrZt(Integer yhsxdrZt) {
        this.yhsxdrZt = yhsxdrZt;
    }


    public String getCzr() {
        return czr;
    }

    public void setCzr(String czr) {
        this.czr = czr;
    }

    public Long getYhsxdrId() {
        return yhsxdrId;
    }

    public void setYhsxdrId(Long yhsxdrId) {
        this.yhsxdrId = yhsxdrId;
    }

    public Yg getYg() {
        return yg;
    }

    public void setYg(Yg yg) {
        this.yg = yg;
    }


    public Map<String, Object> getVoucherMaster() {
        return voucherMaster;
    }

    public void setVoucherMaster(Map<String, Object> voucherMaster) {
        this.voucherMaster = voucherMaster;
    }

    public Integer getAttachment() {
        return attachment;
    }

    public void setAttachment(Integer attachment) {
        this.attachment = attachment;
    }

    public XmlParse getXmlParse() {
        return xmlParse;
    }

    public void setXmlParse(XmlParse xmlParse) {
        this.xmlParse = xmlParse;
    }

    public Map<String, String> getResult() {
        return result;
    }

    public void setResult(Map<String, String> result) {
        this.result = result;
    }

    public Gson getGson() {
        return gson;
    }

    public void setGson(Gson gson) {
        this.gson = gson;
    }

    public Date getYhsxdrSystemDate() {
        return yhsxdrSystemDate;
    }

    public void setYhsxdrSystemDate(Date yhsxdrSystemDate) {
        this.yhsxdrSystemDate = yhsxdrSystemDate;
    }

    public Map<String, Object> getVoucher() {
        return voucher;
    }

    public void setVoucher(Map<String, Object> voucher) {
        this.voucher = voucher;
    }

    public List<VoucherItemData> getVoucherItems() {
        return voucherItems;
    }

    public void setVoucherItems(List<VoucherItemData> voucherItems) {
        this.voucherItems = voucherItems;
    }

    public String getBz() {
        return bz;
    }

    public void setBz(String bz) {
        this.bz = bz;
    }

    public String getGsMc() {
        return gsMc;
    }

    public void setGsMc(String gsMc) {
        this.gsMc = gsMc;
    }

    public Date getJkrqStart() {
        return jkrqStart;
    }

    public void setJkrqStart(Date jkrqStart) {
        this.jkrqStart = jkrqStart;
    }

    public Date getJkrqEnd() {
        return jkrqEnd;
    }

    public void setJkrqEnd(Date jkrqEnd) {
        this.jkrqEnd = jkrqEnd;
    }

    public String getVoucherNum() {
        return voucherNum;
    }

    public void setVoucherNum(String voucherNum) {
        this.voucherNum = voucherNum;
    }

    public String getSystemDate() {
        return systemDate;
    }

    public void setSystemDate(String systemDate) {
        this.systemDate = systemDate;
    }

    @Autowired
    public void setYhsxdrService(YhsxdrService yhsxdrService) {
        this.yhsxdrService = yhsxdrService;
    }
    @Autowired
    public void setVoucherService(VoucherService voucherService) {
        this.voucherService = voucherService;
    }
    @Autowired
    public void setYgService(YgService ygService) {
        this.ygService = ygService;
    }
    @Autowired
    public void setYhsxmxDao(YhsxmxDao yhsxmxDao) {
        this.yhsxmxDao = yhsxmxDao;
    }
}
